<template>
	<view class="login">
		<view class="sologn">
			<image src="../../static/image/logo1.png"></image>
			<view class="txt">AI智能对话 </view>
		</view>
		<button class="btn" type="default" @click="getInfo()"> 微信授权一键登录 </button>
		<fWxLogin ref="fwloginRef" :showWxLogin="showWxLogin" @setUserInfoCallback="handleLoginClick"
			@onClose="showWxLogin = false">
		</fWxLogin>
	</view>
</template>
<script>
	import fWxLogin from '@/components/f_wxLogin.vue'
	import config from '@/utils/config.js'
	export default
	{
		components:
		{
			fWxLogin
		},
		data()
		{
			return {
				showWxLogin: false,
				code: '',
				openid: '',
				sessionKey: ''
			}
		},
		onShow()
		{
			uni.hideHomeButton()
			uni.getSystemInfo(
			{
				success: (res) =>
				{
					const
					{
						safeAreaInsets
					} = res
					console.log(safeAreaInsets.bottom);
					uni.setStorageSync('safeBottom', safeAreaInsets.bottom)
					uni.setStorageSync('safeTop', safeAreaInsets.top)
				}
			})
		},
		mounted()
		{},
		methods:
		{
			getInfo()
			{
				this.wxLogin()
			},
			handleLoginClick(userInfo)
			{
				this.getUserInfo(userInfo)
			},
			wxLogin()
			{
				const that = this
				uni.login(
				{
					provider: 'weixin',
					success: async (res) =>
					{
						const res1 = await that.$get('api/login',
						{
							appid: config.appid,
							code: res.code
						},
						{
							"Content-Type": 'application/x-www-form-urlencoded'
						})
						console.log(res1, 'res1');
						if (res1.status == 200)
						{
							that.$refs.fwloginRef.avatarUrl = res1.data.avatarUrl
							that.$refs.fwloginRef.nickName = res1.data.nickName
							that.openid = res1.data.openid
							that.sessionKey = res1.data.sessionKey
							that.showWxLogin = true
						}
					},
					fail()
					{}
				})
			},
			getUserInfo(userInfo)
			{
				uni.showLoading()
				const that = this
				uni.getUserInfo(
				{
					provider: 'weixin',
					success: function(infoRes)
					{
						console.log(infoRes);
						const
						{
							encryptedData,
							iv,
							signature,
							rawData
						} = infoRes
						that.$post('api/update_info',
						{
							appid: config.appid,
							encryptedData,
							iv,
							signature,
							rawData,
							openid: that.openid,
							sessionKey: that.sessionKey,
							avatarUrl: userInfo.avatar_url,
							nickName: userInfo.nick_name
						},
						{
							"Content-Type": 'application/json'
						}).then(res11 =>
						{
							uni.hideLoading()
							console.log(res11, 'res11');
							uni.setStorageSync('openid', that.openid)
							uni.setStorageSync('sessionKey', that.sessionKey)
							uni.setStorageSync('Avatar', userInfo.avatar_url)
							uni.setStorageSync('wxName', userInfo.nick_name)
							const date =res11.data.vipEndDate&&res11.data.vipEndDate.replaceAll('-','/')
							uni.setStorageSync('vipEndDate', date);
							uni.redirectTo(
							{
								url: '/pages/index/index'
							})
						}).catch(err =>
						{
							uni.hideLoading()
							uni.showToast(
							{
								title: '登陆失败',
								icon: 'none'
							})
						})
					},
					fail(err)
					{
						console.log(err, 'err1');
						uni.showToast(
						{
							title: '登陆失败',
							icon: 'none'
						})
					}
				});
			}
		}
	}
</script>
<style scoped lang="scss">
	.sologn {
		text-align: center;
		padding: 242rpx 0 182rpx;

		image {
			width: 220rpx;
			height: 220rpx;
			vertical-align: bottom;
		}

		.txt {
			font-size: 36rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #333333;
			line-height: 50rpx;
		}
	}

	.btn {
		width: 654rpx;
		height: 88rpx;
		background: linear-gradient(222deg, #2CE6FF 0%, #2A87FF 100%);
		box-shadow: 0px 4rpx 16rpx 0px rgba(0, 0, 0, 0.08);
		border-radius: 44rpx;
		color: #fff;
		font-size: 32rpx;
		letter-spacing: 5rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>
